from tkinter import messagebox
from utils.log_handler import logger
import tkinter.messagebox as messagebox

def bind_shortcuts(main_window):
    """绑定 Windows 系统快捷键（提升操作效率）"""
    try:
        # ---------------------- 项目操作快捷键 ----------------------
        # Ctrl+N：新建项目
        main_window.bind("<Control-n>", lambda e: main_window.project_panel.create_project())
        main_window.bind("<Control-N>", lambda e: main_window.project_panel.create_project())  # 大写兼容
        
        # ---------------------- 任务操作快捷键 ----------------------
        # Ctrl+T：新建任务
        main_window.bind("<Control-t>", lambda e: main_window.task_panel.create_task())
        main_window.bind("<Control-T>", lambda e: main_window.task_panel.create_task())
        
        # Ctrl+B：批量修改任务
        main_window.bind("<Control-b>", lambda e: main_window.task_panel.batch_edit_task())
        main_window.bind("<Control-B>", lambda e: main_window.task_panel.batch_edit_task())
        
        # Delete：删除选中任务
        main_window.bind("<Delete>", lambda e: main_window.task_panel.delete_selected_tasks())
        
        # ---------------------- 甘特图操作快捷键 ----------------------
        # Ctrl+G：切换网格线
        main_window.bind("<Control-g>", lambda e: main_window.gantt_panel.toggle_grid())
        main_window.bind("<Control-G>", lambda e: main_window.gantt_panel.toggle_grid())
        
        # Ctrl++：放大甘特图（Windows 兼容：+ 需用 <plus>）
        main_window.bind("<Control-plus>", lambda e: main_window.gantt_panel.zoom_in())
        main_window.bind("<Control-+>", lambda e: main_window.gantt_panel.zoom_in())  # 备选绑定
        
        # Ctrl+-：缩小甘特图
        main_window.bind("<Control-minus>", lambda e: main_window.gantt_panel.zoom_out())
        # 备选绑定方式已存在，不再添加可能无效的绑定
        
        # ---------------------- 数据导出快捷键 ----------------------
        # Ctrl+E：导出 Excel
        main_window.bind("<Control-e>", lambda e: main_window._export_excel())
        main_window.bind("<Control-E>", lambda e: main_window._export_excel())
        
        # Ctrl+P：导出 PDF
        main_window.bind("<Control-p>", lambda e: main_window._export_pdf())
        main_window.bind("<Control-P>", lambda e: main_window._export_pdf())
        
        # Ctrl+I：导入 JSON
        main_window.bind("<Control-i>", lambda e: main_window._import_json())
        main_window.bind("<Control-I>", lambda e: main_window._import_json())
        
        # ---------------------- 窗口操作快捷键 ----------------------
        # Alt+F4：关闭窗口（复用系统默认，但补充提示）
        main_window.bind("<Alt-F4>", lambda e: main_window._on_close())
        
        logger.info("Windows 快捷键绑定完成（支持 Ctrl/N/T/B/G/E/P、Delete 等）")
    except Exception as e:
        logger.error(f"Windows 快捷键绑定失败：{str(e)}")
        messagebox.showwarning("快捷键绑定失败", f"部分快捷键可能无法使用：{str(e)}", parent=main_window)
